define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'adminlte', 'template', 'formSelects-v4.min', 'xm-select', 'jquery.range-min'], function ($, undefined, Backend, Table, Form, Adminlte, Template, formSelects, xmSelect, range) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'course/teach/index',
                    add_url: 'course/teach/add',
                    edit_url: 'course/teach/edit',
                    del_url: 'course/teach/del',
                    multi_url: 'course/teach/multi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'school_name', title: __('学校'), operate: false},
                        {
                            field: 'school_id',
                            title: __('学校'),
                            align: 'left',
                            searchList: $.getJSON('school/searchList'),
                            visible: false
                        },
                        {field: 'type_text', title: __('课程类型')},
                        {field: 'subject_name', title: __('学科'), operate: false},
                        {field: 'teacher_name', title: __('教师'), operate: false},
                        {field: 'classes_count', title: __('任课班级'), operate: false},
                        //启用时间段搜索
                        {
                            field: 'created_at',
                            title: __('Create time'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                commonSearch: true,
                searchFormVisible: true,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);




        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {

                $('.type').change(function () {
                    if ($(this).val() == 1) {
                        $('.label_classes').text('任课班级');
                    } else {
                        $('.label_classes').text('选课班级');
                    }
                });

                var classes = xmSelect.render({
                    el: '#classes',
                    name: 'row[class_ids]',
                    toolbar:{
                        show: true,
                    },
                    autoRow: true,
                    height: '180px',
                    data: []
                });

                $(document).on('change', ".school", function () {

                    $.ajax({
                        type: "POST",
                        url: "teacher/cxSelect",
                        data: {school_id:$(this).val()},
                        dataType: "json",
                        success: function(ret){
                            $(".teacher").html(Template("tplTeacher", ret));
                        }
                    });

                    $.ajax({
                        type: "POST",
                        url: "course/subject/cxSelect",
                        data: {school_id:$(this).val()},
                        dataType: "json",
                        success: function(ret){
                            $(".subject").html(Template("tplSubject", ret));
                        }
                    });

                    $.ajax({
                        type: "POST",
                        url: "classes/xmSelect",
                        data: {school_id:$(this).val()},
                        dataType: "json",
                        success: function(ret){
                            classes.update({
                                data: ret.data,
                                autoRow: true,
                            });

                            if (Config.admin.class_ids) {
                                classes.setValue(Config.admin.class_ids);
                            }
                        }
                    });


                });

                $('.single-slider').jRange({
                    from: 0,
                    to: 10,
                    step: 1,
                    scale: [0,5,10],
                    format: '%s',
                    width: 300,
                    showLabels: true,
                    snap: true,
                    onstatechange: function (e) {
                        $('.time').html(e)
                    }
                });

                Form.api.bindevent($("form[role=form]"), null, null, function () {
                    return true;
                });
            },
        }
    };
    return Controller;
});